package com.yuncheng.spcyApi.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.UserPxjlBo;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.ClientJumpUrlConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.jcy.JcyLxjhConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.mapper.SpcyPxJbxxMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.common.base.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.StartEndTimeVo;
import com.yuncheng.spcyApi.vo.sqsx.PxCjryJbxxVo;
import com.yuncheng.spcyApi.vo.sqsx.PxSendSmsToCjryVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

/**
 * @Description: 审评查验-培训基本信息
 * @Author: yuncheng
 * @Date: 2023-04-14
 * @Version: V1.0
 */
@Api(tags = "审评查验-培训基本信息接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyPxJbxx")
public class SpcyPxJbxxController extends BaseController<SpcyPxJbxx, ISpcyPxJbxxService> {

    private static final Logger log = LoggerFactory.getLogger(SpcyPxJbxxController.class);

    @Resource
    private ISpcyPxJbxxService spcyPxJbxxService;

    @Resource
    private SpcyPxJbxxMapper spcyPxJbxxMapper;

    @Resource
    private ISpcyPxCjryService spcyPxCjryService;

    @Resource
    private ISysUserPxjlService sysUserPxjlService;

    @Resource
    private IYcSysUserService ycSysUserService;

    @Resource
    private ISpcyPxSjjbxxService spcyPxSjjbxxService;

    @Resource
    private ISpcyPxDjjbxxService spcyPxDjjbxxService;

/*    @Resource
    private ISpcyPxDjtmService spcyPxDjtmService;

    @Resource
    private ISpcyPxDjdaService spcyPxDjdaService;*/

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private SmsUtils smsUtils;

    @Resource
    private ClientJumpUrlConstant clientJumpUrlConstant;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private MyExcelUtils myExcelUtils;

    @Log(title = "继续教育-分页列表查询")
    @ApiOperation(value = "继续教育-分页列表查询")
    @GetMapping(value = "/queryPageList")
    public HttpResult<?> queryPageList(SpcyPxJbxx spcyPxJbxx,
                                       @RequestParam(value = "year",defaultValue = "")String year,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       OrderItemVo orderItemVo) {
        Page<SpcyPxJbxx> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyPxJbxx> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isBlank(spcyPxJbxx.getFpxlx())){
            spcyPxJbxx.setFpxlx(JcyLxjhConstant.JCYLX_PXLX_JXJYPX);
        }
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxJbxx.getFpxdd()), SpcyPxJbxx::getFpxdd,spcyPxJbxx.getFpxdd());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxJbxx.getFpxdj()),SpcyPxJbxx::getFpxdj,spcyPxJbxx.getFpxdj());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxJbxx.getFpxlx()),SpcyPxJbxx::getFpxlx,spcyPxJbxx.getFpxlx());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxJbxx.getFpxmc()),SpcyPxJbxx::getFpxmc,spcyPxJbxx.getFpxmc());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxJbxx.getFpxzjr()),SpcyPxJbxx::getFpxzjr,spcyPxJbxx.getFpxzjr());
        if (spcyPxJbxx.getFpxkssj() != null){
            queryWrapper.le(SpcyPxJbxx::getFpxkssj,spcyPxJbxx.getFpxkssj());
        }
        if (spcyPxJbxx.getFpxjssj() != null){
            queryWrapper.ge(SpcyPxJbxx::getFpxjssj,spcyPxJbxx.getFpxjssj());
        }
        if (StringUtils.isNotBlank(year)){
            queryWrapper.apply("to_char(fpxkssj,'yyyy') = {0}",year);
        }
        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByDesc(SpcyPxJbxx::getCreateTime);
            queryWrapper.orderByDesc(SpcyPxJbxx::getFpxkssj);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<SpcyPxJbxx> pageList = spcyPxJbxxService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "继续教育-添加")
    @ApiOperation(value = "继续教育-添加")
    @PostMapping(value = "/addPxjbxx")
    public HttpResult<?> addPxjbxx(@RequestBody SpcyPxJbxx spcyPxJbxx) {

        spcyPxJbxx.setId(GetUuIdUtils.ReplaceUuId());
        spcyPxJbxx.setFpxlx(JcyLxjhConstant.JCYLX_PXLX_JXJYPX);
        spcyPxJbxx.setFpxsjid("0"); // 默认为0：空
        spcyPxJbxxService.save(spcyPxJbxx);

        String operContent = "新增信息：";
        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyPxJbxx);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_PX_JBXX,operContent,spcyPxJbxx.getId(), "继续教育-添加", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return HttpResultVo.HttpResultSave(spcyPxJbxx, "保存成功");
    }

    @Log(title = "继续教育-编辑")
    @ApiOperation(value = "继续教育-编辑")
    @ApiImplicitParam(name = "id",value = "培训基本信息id",required = true)
    @PutMapping(value = "/editPxjbxx")
    public HttpResult<?> editPxjbxx(@RequestBody SpcyPxJbxx spcyPxJbxx) {
        if (StringUtils.isEmpty(spcyPxJbxx.getId())){
            return HttpResult.error("培训基本信息id不能为空");
        }
        String jbxxId = spcyPxJbxx.getId();
        SpcyPxJbxx oldJbxx= spcyPxJbxxService.getById(jbxxId);

        if (oldJbxx == null){
            return HttpResult.error("当前培训基本信息数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldJbxx);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            BeanUtils.populate(oldJbxx, BeanUtils.describe(spcyPxJbxx));
            oldJbxx.setFpxkssj(spcyPxJbxx.getFpxkssj());
            oldJbxx.setFpxjssj(spcyPxJbxx.getFpxjssj());
        } catch (IllegalAccessException e) {
            log.error("出错【IllegalAccessException】- {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("出错【InvocationTargetException】- {}",e.getMessage());
        } catch (NoSuchMethodException e) {
            log.error("出错【NoSuchMethodException】- {}",e.getMessage());
        }
        spcyPxJbxxService.updateById(oldJbxx);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(oldJbxx,oldMap);
        spcyLogService.addLogs(LogConstant.LOGS_PX_JBXX,operContent,jbxxId,LogConstant.LOGS_PX_JBXX,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }

    @Log(title = "继续教育-批量删除")
    @ApiOperation(value = "继续教育-批量删除")
    @ApiImplicitParam(name = "ids",value = "培训基本信息id,逗号拼接",required = true)
    @DeleteMapping(value = "/deleteBatchPxJbxx")
    public HttpResult<?> deleteBatchPxJbxx(@RequestParam(value = "ids",defaultValue = "") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }

        List<String> idsList = StringPatternUtils.getListByIds(ids);
        List<SpcyPxJbxx> pxJbxxList = spcyPxJbxxService.listByIds(idsList);
        if (CollectionUtils.isEmpty(pxJbxxList)){
            return HttpResult.error("数据不存在，请重试");
        }

        String operContent = "";
        for (SpcyPxJbxx jbxx : pxJbxxList){
            String id = jbxx.getId();
            spcyPxJbxxService.removeById(id); // 删除培训基本信息

            spcyPxCjryService.remove(new LambdaQueryWrapper<SpcyPxCjry>().eq(SpcyPxCjry::getFpxjbxxid,id)); // 删除参加培训人员

            // 培训相关的答卷
            List<SpcyPxDjjbxx> djList = spcyPxDjjbxxService.list(new LambdaQueryWrapper<SpcyPxDjjbxx>().eq(SpcyPxDjjbxx::getFpxjbxxid, id));
            spcyPxDjjbxxService.deletePxdj(djList, id, LogConstant.LOGS_PX_JBXX, "删除培训基本信息");

            operContent = "删除信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(jbxx);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs("删除培训基本信息",operContent,id,LogConstant.LOGS_PX_JBXX,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "培训参加人员-批量删除")
    @ApiOperation(value = "培训参加人员-批量删除")
    @ApiImplicitParam(name = "ids",value = "培训参加人员记录id,逗号拼接",required = true)
    @DeleteMapping(value = "/deletePxCjry")
    public HttpResult<?> deletePxCjry(@RequestParam(value = "ids",defaultValue = "")String ids){
        if(StringUtils.isBlank(ids)){
            return HttpResult.error("请选择需要删除的人员！");
        }
        List<String> idsList = Arrays.asList(ids.split(",|，"));
        if (CollectionUtils.isEmpty(idsList)){
            return HttpResult.error("请选择需要删除的人员");
        }

        List<SpcyPxCjry> pxCjryList = spcyPxCjryService.listByIds(idsList);
        StringBuffer logNr = null;
        if (CollectionUtils.isNotEmpty(pxCjryList)) {
            for (SpcyPxCjry cjry : pxCjryList) {
                    logNr = new StringBuffer();

                    spcyPxCjryService.removeById(cjry.getId());

                    // 查询参加人员的答卷信息
                    LambdaQueryWrapper<SpcyPxDjjbxx> wrapper = new LambdaQueryWrapper<>();
                    if (StringUtils.isNotBlank(cjry.getFuserid())){
                        wrapper.eq(SpcyPxDjjbxx::getFuserid, cjry.getFuserid());
                        wrapper.eq(StringUtils.isNotBlank(cjry.getFpxjbxxid()), SpcyPxDjjbxx::getFpxjbxxid, cjry.getFpxjbxxid());
                    }else {
                        wrapper.eq(SpcyPxDjjbxx::getFuserid, cjry.getId());
                        wrapper.eq(StringUtils.isNotBlank(cjry.getFpxjbxxid()), SpcyPxDjjbxx::getFpxjbxxid, cjry.getFpxjbxxid());
                    }
                    // 培训相关的答卷
                    List<SpcyPxDjjbxx> djList = spcyPxDjjbxxService.list(wrapper);

                    spcyPxDjjbxxService.deletePxdj(djList, pxCjryList.get(0).getFpxjbxxid(), LogConstant.LOGS_PX_JBXX,"删除参加培训的人员");

                    logNr.append("删除人员：").append("【姓名】为[").append(pxCjryList.get(0).getRealname()).append("];");

                    spcyLogService.addLogs(LogConstant.LOGS_PX_JBXX, logNr.toString(), pxCjryList.get(0).getFpxjbxxid(), "删除参加培训的人员", SpcyConstant.LOGS_SYS_TYPE_SC);
                }
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "培训基本信息-通过id查询")
    @ApiOperation(value = "培训基本信息-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam(value = "id",defaultValue = "") String id) {
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }
        Map resultMap = new HashMap();
        // 当前培训信息
        SpcyPxJbxx spcyPxJbxx = spcyPxJbxxService.getById(id);
        resultMap.put("pxJbxx", spcyPxJbxx);

        // 查看培训参加的人员

        // 个人培训记录
        UserPxjlBo userPxjl = new UserPxjlBo();
        userPxjl.setFpxjbxxid(id);
        List<SysUserPxjl> userPxjlList = sysUserPxjlService.getList(userPxjl, "", false);
        resultMap.put("userPxjlList", userPxjlList);

        return HttpResult.ok(resultMap);
    }

    /*@Log(title = "获取培训学时")
    @ApiOperation(value = "获取培训学时")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "fpxkssj",value = "开始时间(yyyy-MM-dd)",required = true),
            @ApiImplicitParam(name = "fpxjssj",value = "结束时间(yyyy-MM-dd)",required = true)
    })
    @GetMapping(value = "/getPxXs")*/
    public HttpResult<?> getPxXs(@RequestParam("fpxkssj") String fpxkssj,@RequestParam("fpxjssj") String fpxjssj){
        if (StringUtils.isBlank(fpxkssj)){
            return HttpResult.error("开始时间不能为空");
        }
        if (StringUtils.isBlank(fpxjssj)){
            return HttpResult.error("结束时间不能为空");
        }
        Date kssj = DateConversionUtils.convertStringToDateYmd(fpxkssj);
        Date jssj = DateConversionUtils.convertStringToDateYmd(fpxjssj);
        long betweenDay = DateUtil.between(kssj, jssj, DateUnit.DAY);
        int betDay = new Long(betweenDay).intValue();

        BigDecimal day = new BigDecimal(betDay);
        BigDecimal xs = new BigDecimal(8);
        BigDecimal pxxs = null;
        pxxs = day.multiply(xs);
        log.info("默认一天为8学时，最终【"+day+"】相应的学时有【"+pxxs+"】");

        Map map = new HashMap();
        map.put("fpxxs",pxxs);
        return HttpResult.ok(map);
    }

    @Log(title = "选择添加参加培训的人员")
    @ApiOperation(value = "选择添加参加培训的人员")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "培训基本信息id",required = true),
            @ApiImplicitParam(name = "fuserids",value = "选择的用户id,多个用户[英文逗号]拼接",required = true)
    })
    @PostMapping(value = "/selectCjryToPx")
    public HttpResult<?> selectCjryToPx(@RequestBody HashMap<String,String> map){
        String pxjbxxid = map.get("id");
        String fuserids = map.get("fuserids");
        if (StringUtils.isBlank(pxjbxxid)){
            return HttpResult.error("参数为空！");
        }
        if (StringUtils.isBlank(fuserids)){
            return HttpResult.error("选择的用户为空，请选择用户");
        }

        List<String> userIdList = StringPatternUtils.getListByIds(fuserids);
        List<YcSysUser> sysUserList = ycSysUserService.listByIds(userIdList);
        if (CollectionUtils.isNotEmpty(sysUserList)) {
            SpcyPxCjry cjry = null;
            String logNr = "";
            for (YcSysUser user : sysUserList) {
                // 验证人员是否已经参加
                List<SpcyPxCjry> cjryList = spcyPxCjryService.list(new LambdaQueryWrapper<SpcyPxCjry>()
                        .eq(SpcyPxCjry::getFuserid, user.getId())
                        .eq(SpcyPxCjry::getFpxjbxxid, pxjbxxid));
                if (CollectionUtils.isEmpty(cjryList)) {
                    cjry = new SpcyPxCjry();
                    BeanUtil.copyProperties(user,cjry);
                    cjry.setId(GetUuIdUtils.ReplaceUuId());
                    cjry.setFpxjbxxid(pxjbxxid);
                    cjry.setFuserid(user.getId());
                    cjry.setFdxfszt(SpcyConstant.WJJL_ZT_DX_NO);
                    spcyPxCjryService.save(cjry);

                    logNr = "选择参加培训人员：";
                    logNr += "[姓名]【" + user.getRealname() + "】";
                    logNr += "[培训信息id]【" + pxjbxxid + "】";
                    spcyLogService.addLogs(LogConstant.LOGS_PX_CJRY, logNr, pxjbxxid, LogConstant.LOGS_PX_JBXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
                }
            }
        }else {
            return HttpResult.error("选择的人员不存在");
        }

        return HttpResult.ok("选择成功");
    }

    @Log(title = "查看培训参加的人员")
    @ApiOperation(value = "查看培训参加的人员")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "培训基本信息id",required = true),
            @ApiImplicitParam(name = "realname",value = "姓名"),
            @ApiImplicitParam(name = "fgzdw",value = "工作单位")

    })
    @GetMapping(value = "viewPxCjry")
    public HttpResult<?> viewPxCjry(@RequestParam("id")String id,
                                    @RequestParam(value = "realname",defaultValue = "")String realname,
                                    @RequestParam(value = "fgzdw",defaultValue = "")String fgzdw,
                                    @RequestParam(value = "phone",defaultValue = "")String phone){

        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }
        // 自动生成培训人员，成绩自动生成考核结果
        this.autoScCjryKhjg(id);

        PxCjryJbxxVo jbxxVo = new PxCjryJbxxVo();
        jbxxVo.setFpxjbxxid(id);
        jbxxVo.setRealname(realname);
        jbxxVo.setFgzdw(fgzdw);
        jbxxVo.setPhone(phone);
        List<PxCjryJbxxVo> pxCjryList = spcyPxJbxxMapper.getPxCjryList(jbxxVo);

        Map resultMap = new HashMap();
        resultMap.put("total", pxCjryList.size());
        resultMap.put("records", pxCjryList);
        return HttpResult.ok(resultMap);
    }

    @Log(title = "更新参加人员信息")
    @ApiOperation(value = "更新参加人员信息")
    @PostMapping(value = "updateCjryXx")
    public HttpResult<?> updateCjryXx(@RequestBody PxCjryJbxxVo cjryJbxxVo){
        if (StringUtils.isBlank(cjryJbxxVo.getId())){
            return HttpResult.error("培训人员参数，不能为空");
        }
        SpcyPxCjry cjry = spcyPxCjryService.getById(cjryJbxxVo.getId());
        if (cjry == null){
            return HttpResult.error("当前参加人员记录不存在，请刷新重试");
        }
        // 人员联系电话修改

        // 参加人员信息修改
        boolean isUpdateCjry = false;
        if (StringUtils.isNotBlank(cjryJbxxVo.getFbz())){
            cjry.setFbz(cjryJbxxVo.getFbz());
            isUpdateCjry = true;
        }
        if (StringUtils.isNotBlank(cjryJbxxVo.getFsfrk())){
            cjry.setFsfrk(cjryJbxxVo.getFsfrk());
            isUpdateCjry = true;
        }
        if (StringUtils.isNotBlank(cjryJbxxVo.getFsfcjbcpx())){
            cjry.setFsfcjbcpx(cjryJbxxVo.getFsfcjbcpx());
            isUpdateCjry = true;
        }
        if (StringUtils.isNotBlank(cjryJbxxVo.getPhone())){
            cjry.setPhone(cjryJbxxVo.getPhone());
            isUpdateCjry = true;
        }
        if (StringUtils.isNotBlank(cjryJbxxVo.getEmail())){
            cjry.setEmail(cjryJbxxVo.getEmail());
            isUpdateCjry = true;
        }
        if (StringUtils.isNotBlank(cjryJbxxVo.getFgzdw())){
            cjry.setFgzdw(cjryJbxxVo.getFgzdw());
            isUpdateCjry = true;
        }
        if (isUpdateCjry){
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(cjry);
            boolean flag = spcyPxCjryService.updateById(cjry);
            if (flag) {
                String log = LogUtils.BeanModifyMsg(cjry, oldMap);
                spcyLogService.addLogs(LogConstant.LOGS_PX_JBXX, log, cjry.getFpxjbxxid(), SpcyConstant.LOGS_SYS_TYPE_XG, SpcyConstant.LOGS_SYS_TYPE_XG);
            }
        }

        return HttpResult.ok("保存成功");
    }

    @Log(title = "查询培训试卷列表")
    @ApiOperation(value = "查询培训试卷列表")
    @GetMapping(value = "queryPxSjjbxxList")
    public HttpResult<?> queryPxSjjbxxList(SpcyPxSjjbxx spcyPxSjjbxx, PageVo pageVo){
        Page<SpcyPxSjjbxx> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<SpcyPxSjjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(spcyPxSjjbxx.getId()), SpcyPxSjjbxx::getId, spcyPxSjjbxx.getId());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjjbxx.getFsjlx()),SpcyPxSjjbxx::getFsjlx,spcyPxSjjbxx.getFsjlx());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxSjjbxx.getFsjmc()),SpcyPxSjjbxx::getFsjmc,spcyPxSjjbxx.getFsjmc());
        queryWrapper.eq(spcyPxSjjbxx.getFjzrq() != null,SpcyPxSjjbxx::getFjzrq,spcyPxSjjbxx.getFjzrq());

        queryWrapper.orderByDesc(SpcyPxSjjbxx::getCreateTime);
        IPage<SpcyPxSjjbxx> pageList = spcyPxSjjbxxService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    @Log(title = "保存-选择的培训试卷")
    @ApiOperation(value = "保存-选择的培训试卷")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "培训基本信息id",required = true),
            @ApiImplicitParam(name = "sjid",value = "试卷id",required = true)
    })
    @GetMapping(value = "selectPxsj")
    public HttpResult<?> selectPxsj(@RequestParam(value = "id",defaultValue = "")String id, @RequestParam(value = "sjid",defaultValue = "") String sjid){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("培训基本信息参数丢失，请联系管理员");
        }
        if (StringUtils.isBlank(sjid)){
            return HttpResult.error("请选择试卷");
        }
        SpcyPxJbxx pxJbxx = spcyPxJbxxService.getById(id);
        if (pxJbxx == null){
            return HttpResult.error("当前培训记录不存在，请刷新重试");
        }
        pxJbxx.setFpxsjid(sjid);
        spcyPxJbxxService.updateById(pxJbxx);

        return HttpResult.ok("选择试卷成功");
    }

    @Log(title = "移除已选择的培训试卷")
    @ApiOperation(value = "移除已选择的培训试卷")
    @GetMapping(value = "removePxjlSelectSj")
    public HttpResult<?> removePxjlSelectSj(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }
        SpcyPxJbxx pxJbxx = spcyPxJbxxService.getById(id);
        if (pxJbxx == null){
            return HttpResult.error("没有相匹配的数据！");
        }
        pxJbxx.setFpxsjid("0");
        spcyPxJbxxService.updateById(pxJbxx);

        return HttpResult.ok("移除成功");
    }

    @Log(title = "给参加培训的人员发送试卷")
    @ApiOperation(value = "给参加培训的人员发送试卷")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "培训基本信息id",required = true)
    })
    @GetMapping(value = "sendSjToPxCjry")
    public HttpResult<?> sendSjToPxCjry(@RequestParam("id")String id){
        HttpResult<?> httpResult = this.sendSj(id);
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        return HttpResult.ok("发放成功");
    }

    // 给参加培训的人员发送试卷 如果用户不存在系统，就绑定人员记录id, 否则，绑定用户id
    public HttpResult<?> sendSj(String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("培训基本信息参数丢失，请联系管理员");
        }
        SpcyPxJbxx jbxx = spcyPxJbxxService.getById(id);
        if (jbxx == null){
            return HttpResult.error("培训基本信息不存在，请刷新重试");
        }
        if (StringUtils.isBlank(jbxx.getFpxsjid())){
            return HttpResult.error("发送试卷失败，请先选择试卷");
        }
        String sjid = jbxx.getFpxsjid();
        List<SpcyPxCjry> cjryList = spcyPxCjryService.list(new LambdaQueryWrapper<SpcyPxCjry>().eq(SpcyPxCjry::getFpxjbxxid, id));
        if (CollectionUtils.isEmpty(cjryList)){
            return HttpResult.error("当前培训没有参加人员，请选择参加人员");
        }

        if (CollectionUtils.isNotEmpty(cjryList)){
            SpcyPxDjjbxx djjbxx = null;
            for (SpcyPxCjry cjry : cjryList){
                djjbxx = new SpcyPxDjjbxx();
                djjbxx.setFpxjbxxid(id);

                // 如何用户不存在账号，就绑定参加人员记录id
                if (StringUtils.isNotBlank(cjry.getFuserid())) {
                    djjbxx.setFuserid(cjry.getFuserid());
                }else {
                    djjbxx.setFuserid(cjry.getId());
                }
                djjbxx.setFdjffsj(new Date());
                djjbxx.setFdjffzt(SpcyConstant.PX_DJFFZT_YES);
                djjbxx.setFsjid(sjid);
                djjbxx.setFksxm(cjry.getRealname());
                djjbxx.setFlxdh(cjry.getPhone());
                djjbxx.setFgzdw(cjry.getFgzdw());

                HttpResult result = this.submitDjjbxx(djjbxx);// 发送试卷
                if (!result.isSuccess()){
                    return HttpResult.error(result.getMessage());
                }
            }
        }

        return HttpResult.ok("发放成功");
    }

    // 自动生成培训人员，成绩自动生成考核结果
    public void autoScCjryKhjg(String id){

        List<SpcyPxCjry> cjryList = spcyPxCjryService.list(new LambdaQueryWrapper<SpcyPxCjry>()
                .eq(SpcyPxCjry::getFpxjbxxid, id));
        if (CollectionUtils.isNotEmpty(cjryList)){
            for (SpcyPxCjry cjry : cjryList){
                String userid = "";
                if (StringUtils.isBlank(cjry.getFuserid())){
                    userid = cjry.getId();
                }else {
                    userid = cjry.getFuserid();
                }

                // 已经提交试卷
                List<SpcyPxDjjbxx> djxxList = spcyPxDjjbxxService.list(new LambdaQueryWrapper<SpcyPxDjjbxx>()
                        .eq(SpcyPxDjjbxx::getFuserid, userid)
                        .eq(SpcyPxDjjbxx::getFpxjbxxid, id)
                        .isNotNull(SpcyPxDjjbxx::getFtjsj));
                if (CollectionUtils.isNotEmpty(djxxList)){
                    if (StringUtils.isBlank(djxxList.get(0).getFdf()) || Integer.valueOf(djxxList.get(0).getFdf()) < 60){
                        cjry.setFbz("不合格");
                    }else {
                        cjry.setFbz("合格");
                    }
                    spcyPxCjryService.updateById(cjry);
                }
            }
        }

    }

    /*@Log(title = "给参加培训的人员-发送试卷短信")
    @ApiOperation(value = "给参加培训的人员-发送试卷短信")
    @ApiImplicitParam(name = "id",value = "培训基本信息id",required = true)
    @GetMapping(value = "sendSjSmsOld")*/
    public HttpResult<?> sendSjSmsOld(@RequestParam("id")String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("培训基本信息参数丢失，请联系管理员");
        }
        HttpResult<?> httpResult = this.sendSj(id);
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        List<SpcyPxDjjbxx> pxSjList = spcyPxDjjbxxService.list(new LambdaQueryWrapper<SpcyPxDjjbxx>()
                .eq(SpcyPxDjjbxx::getFpxjbxxid, id));
        if (CollectionUtils.isEmpty(pxSjList)){
            return HttpResult.error("请先给人员发送试卷，再进行发送答卷短信");
        }
        SpcyPxJbxx pxJbxx = spcyPxJbxxService.getById(id);
        if (pxJbxx == null){
            return HttpResult.error("培训基本信息不存在，请刷新重试");
        }
        pxJbxx.setFsjfszt("是"); // 试卷发送状态
        spcyPxJbxxService.updateById(pxJbxx);

        StringBuffer sendMsg = new StringBuffer();
        for (SpcyPxDjjbxx dj : pxSjList){
            StringBuffer msg = new StringBuffer("");
            msg.append("尊敬的");
            msg.append(dj.getFksxm()).append("同志：").append("\n");
            msg.append("您参加的");
            if (pxJbxx.getFpxkssj() != null){
                msg.append(DateConversionUtils.DateToString(pxJbxx.getFpxkssj()));
            }
            if (pxJbxx.getFpxkssj() != null){
                msg.append("至").append(DateConversionUtils.DateToString(pxJbxx.getFpxjssj()));
            }
            msg.append("的").append(pxJbxx.getFpxmc()).append("培训。").append("\n");
            msg.append("需要填写相关的试卷，试卷链接如下：").append("\n");
            String httpsUrl = clientJumpUrlConstant.http_url; // 跳转https路径
            msg.append(httpsUrl + ClientJumpUrlConstant.sendSmsByDjxx_Url_1).append(dj.getId());

            if (StringUtils.isBlank(dj.getFlxdh())){
                sendMsg.append("【姓名】为[").append(dj.getFksxm()).append("]的联系电话不存在");
            }

            /** 发送短信 */

        }

        if (StringUtils.isBlank(sendMsg.toString())){
            return HttpResult.ok("发送成功");
        }
        return HttpResult.ok("发送成功，"+sendMsg.toString());
    }

    @Log(title = "给参加培训的人员-发送试卷短信")
    @ApiOperation(value = "给参加培训的人员-发送试卷短信")
    @ApiImplicitParam(name = "id",value = "培训基本信息id",required = true)
    @PostMapping(value = "sendSjSms")
    public HttpResult<?> sendSjSms(@RequestBody PxSendSmsToCjryVo cjryVo){

        if (StringUtils.isBlank(cjryVo.getId())){
            return HttpResult.error("培训基本信息参数丢失，请联系管理员");
        }
        if (StringUtils.isBlank(cjryVo.getIds())){
            return HttpResult.error("请选择参加人员");
        }
        String id = cjryVo.getId(); // 培训基本信息id
        String ids = cjryVo.getIds(); // 参加人员id

        HttpResult<?> httpResult = this.sendSj(id);
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        PxCjryJbxxVo jbxxVo = new PxCjryJbxxVo();
        jbxxVo.setId(ids);
        List<PxCjryJbxxVo> pxSjList = spcyPxJbxxMapper.getPxCjryList(jbxxVo);
        if (CollectionUtils.isEmpty(pxSjList)){
            return HttpResult.error("请先给人员发送试卷，再进行发送答卷短信");
        }
        SpcyPxJbxx pxJbxx = spcyPxJbxxService.getById(id);
        if (pxJbxx == null){
            return HttpResult.error("培训基本信息不存在，请刷新重试");
        }
        pxJbxx.setFsjfszt("是"); // 试卷发送状态
        spcyPxJbxxService.updateById(pxJbxx);

        StringBuffer sendMsg = new StringBuffer();
        for (PxCjryJbxxVo dj : pxSjList){
            StringBuffer msg = new StringBuffer("");
            msg.append("尊敬的");
            msg.append(dj.getRealname()).append("同志：").append("\n");
            msg.append("您参加的");
            if (pxJbxx.getFpxkssj() != null){
                msg.append(DateConversionUtils.DateToString(pxJbxx.getFpxkssj()));
            }
            if (pxJbxx.getFpxjssj() != null){
                msg.append("至").append(DateConversionUtils.DateToString(pxJbxx.getFpxjssj()));
            }
            msg.append("的").append(pxJbxx.getFpxmc()).append("培训。").append("\n");
            msg.append("需要填写相关的试卷，试卷链接如下：").append("\n");
            String httpsUrl = clientJumpUrlConstant.http_url; // 跳转https路径
            msg.append(httpsUrl + ClientJumpUrlConstant.sendSmsByDjxx_Url_1).append(dj.getDjid());

            if (StringUtils.isBlank(dj.getPhone())){
                sendMsg.append("【姓名】为[").append(dj.getPhone()).append("]的联系电话不存在").append("\n");
            }

            /** 发送短信 */

            String flag = "成功";
            if (!flag.contains(BusinessConstant.SMS_FLAG)) {
                log.error("发送试卷短信失败：{}", dj.getPhone());
            }else {
                SpcyPxCjry cjry = spcyPxCjryService.getById(dj.getId());
                if (cjry != null) {
                    cjry.setFdxfszt(SpcyConstant.WJJL_ZT_DX);
                    spcyPxCjryService.updateById(cjry);

                    String log = "发送培训试卷及短信：【姓名】为["+dj.getRealname()+"];";
                    spcyLogService.addLogs(LogConstant.LOGS_PX_JBXX,log,dj.getFpxjbxxid(),SpcyConstant.LOGS_SYS_TYPE_BC,SpcyConstant.LOGS_SYS_TYPE_BC);
                }
            }

        }

        if (StringUtils.isBlank(sendMsg.toString())){
            return HttpResult.ok("发送成功");
        }

        return HttpResult.ok("发送成功，"+sendMsg.toString());
    }

    @Log(title = "批量生成个人培训记录")
    @ApiOperation(value = "批量生成个人培训记录")
    @ApiImplicitParam(name = "ids",value = "参加人员的id，多个人员id【英文逗号】拼接")
    @PostMapping(value = "batchCreateMyPxjl")
    public HttpResult<?> batchCreateMyPxjl(@RequestBody HashMap<String,String> map){
        String ids = map.get("ids");
        if (StringUtils.isBlank(ids)){
            return HttpResult.error("请选择人员");
        }
        List<String> idsList = Arrays.asList(ids.split(",|，"));
        List<SpcyPxCjry> cjryList = spcyPxCjryService.listByIds(idsList);
        if (CollectionUtils.isEmpty(cjryList)){
            return HttpResult.error("没有选择人员，请选择人员");
        }
        String pxid = cjryList.get(0).getFpxjbxxid();

        SpcyPxJbxx jbxx = spcyPxJbxxService.getById(pxid);

        StringBuffer msg = new StringBuffer();
        msg = spcyPxJbxxService.syncGenCjryPxjl(jbxx, cjryList, msg, pxid);

        if (StringUtils.isBlank(msg.toString())){
            return HttpResult.ok("生成个人培训记录成功");
        }else{
            return HttpResult.error(msg.toString());
        }
    }

    @Log(title = "用户培训记录-编辑")
    @ApiOperation(value = "用户培训记录-编辑")
    @ApiImplicitParam(name = "id",value = "用户培训记录id",required = true)
    @PutMapping(value = "/editUserPxjl")
    public HttpResult<?> editUserPxjl(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("用户培训记录id不能为空");
        }
        String djjxxxId = buffer.get("id");
        SysUserPxjl oldPxjlUpdate = sysUserPxjlService.getById(djjxxxId);
        if (oldPxjlUpdate == null){
            return HttpResult.error("当前用户培训记录基本信息数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldPxjlUpdate);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldPxjlUpdate, buffer);
        sysUserPxjlService.updateById(oldPxjlUpdate);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(oldPxjlUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_PXJL,operContent,djjxxxId,LogConstant.LOGS_SYS_USER_PXJL,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }

    @Log(title = "用户培训记录-批量删除")
    @ApiOperation(value = "用户培训记录-批量删除")
    @DeleteMapping(value = "/deleteBatchUserPxjl")
    public HttpResult<?> deleteBatchUserPxjl(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SysUserPxjl> sysUserPxjls = sysUserPxjlService.listByIds(idsList);
        if (CollectionUtils.isEmpty(sysUserPxjls)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SysUserPxjl pxjl : sysUserPxjls){
            String id = pxjl.getId();
            sysUserPxjlService.removeById(id);

            operContent = "删除信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(pxjl);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_PXJL,operContent,id,LogConstant.LOGS_SYS_USER_PXJL,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "查看参加培训人员的答卷详情")
    @ApiOperation(value = "查看参加培训人员的答卷详情")
    @GetMapping(value = "queryPxrySjxxPageList")
    public HttpResult<?> queryPxrySjxxPageList(SpcyPxDjjbxx spcyPxDjjbxx,
                                       StartEndTimeVo startEndTimeVo,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        if (StringUtils.isBlank(spcyPxDjjbxx.getFpxjbxxid())){
            return HttpResult.error("培训参数不能为空");
        }
        Page<SpcyPxDjjbxx> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyPxDjjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFpxjbxxid()),SpcyPxDjjbxx::getFpxjbxxid,spcyPxDjjbxx.getFpxjbxxid());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFsjlx()),SpcyPxDjjbxx::getFsjlx,spcyPxDjjbxx.getFsjlx());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFsjmc()),SpcyPxDjjbxx::getFsjmc,spcyPxDjjbxx.getFsjmc());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFksxm()),SpcyPxDjjbxx::getFksxm,spcyPxDjjbxx.getFksxm());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFgzdw()),SpcyPxDjjbxx::getFgzdw,spcyPxDjjbxx.getFgzdw());
        queryWrapper.like(StringUtils.isNotEmpty(spcyPxDjjbxx.getFlxdh()),SpcyPxDjjbxx::getFlxdh,spcyPxDjjbxx.getFlxdh());
        queryWrapper.like(StringUtils.isNotBlank(spcyPxDjjbxx.getFdf()),SpcyPxDjjbxx::getFdf,spcyPxDjjbxx.getFdf());
        if (startEndTimeVo.getStartTime() != null && startEndTimeVo.getEndTime() != null){
            queryWrapper.between(SpcyPxDjjbxx::getFtjsj,startEndTimeVo.getStartTime(),startEndTimeVo.getEndTime());
        }
        if (StringUtils.isNotBlank(startEndTimeVo.getFlag())) {
            if (startEndTimeVo.getFlag().equals("是")) {
                queryWrapper.isNotNull(SpcyPxDjjbxx::getFtjsj);
            } else {
                queryWrapper.isNull(SpcyPxDjjbxx::getFtjsj);
            }
        }
        queryWrapper.orderByDesc(SpcyPxDjjbxx::getCreateTime);
        queryWrapper.orderByDesc(SpcyPxDjjbxx::getFtjsj);
        IPage<SpcyPxDjjbxx> pageList = spcyPxDjjbxxService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    /**
     * 给用户发送答卷，从原试卷基本信息复制一份给答卷基本信息
     */
    public HttpResult submitDjjbxx(SpcyPxDjjbxx spcyPxDjjbxx) {

        String ysjId = spcyPxDjjbxx.getFsjid();
        List<SpcyPxDjjbxx> existUserList = spcyPxDjjbxxService.list(new LambdaQueryWrapper<SpcyPxDjjbxx>()
                .eq(SpcyPxDjjbxx::getFpxjbxxid, spcyPxDjjbxx.getFpxjbxxid())
                .eq(SpcyPxDjjbxx::getFuserid, spcyPxDjjbxx.getFuserid()));
        if (CollectionUtils.isNotEmpty(existUserList)){
            return HttpResult.ok("操作成功");
        }

        // 初始化-试卷信息
        String djId = spcyPxDjjbxxService.initDjjbxx(spcyPxDjjbxx,ysjId);

        Map map = new HashMap();
        map.put("id",djId);
        return HttpResult.ok(map);
    }

    @Log(title = "下载培训参加人员-导入模板")
    @ApiOperation(value = "下载培训参加人员-导入模板")
    @GetMapping("/downCjryTemplate")
    public void downCjryTemplate(HttpServletResponse response){
        try {
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode("cjryTemplate.xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();
            myExcelUtils.buildImpModel(BusinessConstant.IMPORT_PX_CJRY_XML, out);
        } catch (Exception e) {
            log.error("下载编号规则库模板错误-{}",e.getMessage());
        }
    }

    @Log(title = "导入培训参加人员数据-表格excel")
    @ApiOperation(value = "导入培训参加人员数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file",value = "excel模板文件",required = true),
            @ApiImplicitParam(name = "fpxjbxxid",value = "培训基本信息id",required = true),
    })
    @PostMapping(value = "impPxCjryData")
    public HttpResult<?> impPxCjryData(@RequestParam("file") MultipartFile file, @RequestParam("fpxjbxxid")String fpxjbxxid){
        if (file.isEmpty()){
            return HttpResult.error("文件不存在");
        }
        if (StringUtils.isBlank(fpxjbxxid)){
            return HttpResult.error("导入失败，导入参数不能为空");
        }
        SpcyPxJbxx pxxx = spcyPxJbxxService.getById(fpxjbxxid);
        if (pxxx == null){
            return HttpResult.error("导入失败，当前培训信息不存在，请刷新重试，并联系管理员");
        }

        //获取文件名
        String fileName = file.getOriginalFilename();
        //判断文件类型，这里我们只要xls文件
        if (fileName != null && !fileName.endsWith(".xls") && !fileName.endsWith(".xlsx"))
            return HttpResult.error(1, "请选择“xls”或“xlsx”文件");

        File fileUpload = localFileUploadUtils.uploadDempFile(file,fileName);
        // 获取导入的数据
        List<Map<String, Object>> mapsList = null;
        try {
            mapsList = myExcelUtils.paresExclToMapList(BusinessConstant.IMPORT_PX_CJRY_XML, fileUpload);
        } catch (Exception e) {
            log.error("导入培训参加人员数据出错-{}",e.getMessage());
        }
        StringBuffer msg = new StringBuffer();
        if (CollectionUtils.isNotEmpty(mapsList) && mapsList.size() > 0) {
            int i = 1;
            for (Map map : mapsList) {
                String realname = map.get("realname").toString(); // 姓名
                String fgzdw = map.get("fgzdw").toString(); // 工作单位
                String phone = map.get("phone").toString(); // 电话1
                String phone2 = map.get("phone2").toString(); // 电话2
                String email = map.get("email").toString(); // 邮箱

                // 去掉全角空格
                if (StringUtils.isBlank(realname)){
                    msg.append("第").append(i).append("条").append("【姓名】为空, 无法导入该人员").append("\n");
                    continue;
                }
                realname = TrimUtils.trimString(realname);

                if (StringUtils.isBlank(phone)){
                    msg.append("第").append(i).append("条").append("【姓名】为[").append(realname).append("]的电话1为空，无法导入该人员").append("\n");
                    continue;
                }
                phone = TrimUtils.trimString(phone);
                phone2 = TrimUtils.trimString(phone2);

                // 根据姓名，联系电话1、联系电话2
                String lxdh1 = phone;
                String lxdh2 = phone2;
                List<YcSysUser> nowUserList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                        .eq(YcSysUser::getRealname, realname)
                        .and(f -> f.eq(StringUtils.isNotBlank(lxdh1), YcSysUser::getPhone, lxdh1)
                                .or()
                                .isNull(YcSysUser::getPhone)
                                .or()
                                .eq(StringUtils.isNotBlank(lxdh2),YcSysUser::getPhone,lxdh2)
                        ));
                YcSysUser nowUser = null;
                if (CollectionUtils.isEmpty(nowUserList)){
                    msg.append("第").append(i).append("条").append("【姓名】为[").append(realname).append("]不存在系统中找不到该人员的账号！").append("\n");

                    // 新增核查员部门下的账号
                    /*YcSysUser user = new YcSysUser();
                    user.setUsername(realname);
                    user.setRealname(realname);
                    user.setFgzdw(fgzdw);
                    user.setPhone(phone);
                    user.setEmail(email);

                    List<YcSysUser> addUserList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                            .eq(YcSysUser::getUsername, user.getUsername())
                            .eq(YcSysUser::getRealname, user.getRealname()));
                    if (CollectionUtils.isNotEmpty(addUserList)) {
                        msg.append("第").append(i).append("条").append("【姓名】为[").append(realname);
                        msg.append("]不存在系统账号, 新增账号失败，【账号】为[").append(user.getUsername()).append("]\n");
                        continue;
                    }
                    nowUser = hcyService.addHcyUser(user, SpcyConstant.HCY_USERTYPE_JCY, null);*/
                }else {
                    nowUser = nowUserList.get(0);
                }

                boolean isAddFlag = true; // 判断是否新增参加人员
                SpcyPxCjry impCjry = new SpcyPxCjry();
                impCjry.setId(GetUuIdUtils.ReplaceUuId());
                impCjry.setFgzdw(fgzdw);
                if (nowUser == null){
                    impCjry.setRealname(realname);
                    isAddFlag = false;
                }else {
                    impCjry.setFzc(nowUser.getFzc());
                    impCjry.setRealname(nowUser.getRealname());
                    impCjry.setFuserid(nowUser.getId());
                }
                impCjry.setPhone(phone);
                impCjry.setEmail(email);
                impCjry.setFpxjbxxid(fpxjbxxid);

                // 判断是否已经存在参加人员
                List<SpcyPxCjry> cjryList = new ArrayList<>();
                if (isAddFlag) {
                    cjryList  = spcyPxCjryService.list(new LambdaQueryWrapper<SpcyPxCjry>()
                            .eq(SpcyPxCjry::getFpxjbxxid, fpxjbxxid)
                            .eq(SpcyPxCjry::getFuserid, nowUser.getId()));

                    if (CollectionUtils.isNotEmpty(cjryList)){
                        continue;
                    }
                }

                impCjry.setFdxfszt(SpcyConstant.WJJL_ZT_DX_NO);
                spcyPxCjryService.save(impCjry);
                i++;

                String operContent = "新增导入培训参加人员信息：";
                Map<String, Object> bhgzMap = ObjectToMapUtils.objectToMap(impCjry);
                operContent += LogUtils.MapToStringMethod(bhgzMap);
                spcyLogService.addLogs(LogConstant.LOGS_PX_JBXX,operContent,fpxjbxxid,LogConstant.LOGS_PX_JBXX,SpcyConstant.LOGS_SYS_TYPE_XZ);
            }
        }else {
            msg.append("导入成功，导入文件数据为空，请填写内容。");
        }
        fileUpload.delete();
        if (StringUtils.isBlank(msg.toString())){
            msg.append("导入成功");
        }
        return HttpResult.ok(msg.toString());
    }
}
